package com.danco.training.zaneuskaya.dependency;

import java.io.Serializable;
import java.util.HashMap;

import org.apache.log4j.Logger;

public class DependencyConfiguration implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3502084704829594490L;

	private static final Logger logger = Logger
			.getLogger(DependencyConfiguration.class);

	public static DependencyContainer dependencyContainer = DependencyContainer
			.getDependencyContainerInstance();

	public static void fillContainer() {
		HashMap<String, String> result = dependencyContainer.getServices();
		result.put("com.danco.training.zaneuskaya.api.BookService",
				"com.danco.training.zaneuskaya.service.impl.BookServiceImpl");
		result.put("com.danco.training.zaneuskaya.api.OrderService",
				"com.danco.training.zaneuskaya.service.impl.OrderServiceImpl");
		result.put("com.danco.training.zaneuskaya.api.ClaimService",
				"com.danco.training.zaneuskaya.service.impl.ClaimServiceImpl");
		result.put("com.danco.training.zaneuskaya.api.BookController",
				"com.danco.training.zaneuskaya.controller.BookControllerImpl");
		result.put("com.danco.training.zaneuskaya.api.OrderController",
				"com.danco.training.zaneuskaya.controller.OrderControllerImpl");
		result.put("com.danco.training.zaneuskaya.api.ClaimController",
				"com.danco.training.zaneuskaya.controller.ClaimControllerImpl");
		result.put("com.danco.training.zaneuskaya.api.MenuController",
				"com.danco.training.zaneuskaya.controller.MenuControllerImpl");
	
		dependencyContainer.setServices(result);

	}

	public static Object getInstanceByClassName(Class<?> className) {
		fillContainer();
		String instanceClassName = dependencyContainer.getServices().get(
				className.getCanonicalName());
		Object object = null;
		try {
			object = Class.forName(instanceClassName).newInstance();
		} catch (InstantiationException e) {
			logger.error(e.getMessage(), e);
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage(), e);
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage(), e);
		}
		return object;

	}

}
